local function GetStringsTable()
    return TUNING.NPC_CHAT_TABLE[ type(TUNING.__NPC_LANGUAGE)=="function" and TUNING.__NPC_LANGUAGE() or TUNING.__NPC_LANGUAGE ] ["npc_event_item_buyer"] or {}
end

local function create_common(build, scale, tag)
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddDynamicShadow()
    inst.entity:AddNetwork()

    MakeCharacterPhysics(inst, 50, .5)

    inst.DynamicShadow:SetSize(2.5, 1.5)
    inst.Transform:SetFourFaced()
    inst.Transform:SetScale(scale, scale, scale)

    inst.AnimState:SetBank("walrus")
    inst.AnimState:SetBuild(build)
    --inst.AnimState:Hide("hat")

    -- inst:AddTag("character")
    -- inst:AddTag("walrus")
    -- inst:AddTag("houndfriend")
    if tag ~= nil then
        inst:AddTag(tag)
    end

    inst.entity:SetPristine()
    

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("npc_base_lib")
    inst:AddComponent("npc_everything_data")

    -- inst:AddComponent("inspectable") --可检查组件
    inst:AddComponent("named") --名字组件
    inst.components.named.possiblenames = inst.npc_base_lib:Get_Names()
    inst.components.named:PickNewName()


    inst:AddComponent("locomotor")
    inst.components.locomotor.runspeed = 4
    inst.components.locomotor.walkspeed = 2

    inst:SetStateGraph("SGwalrus")
    inst.soundgroup = "mctusk"


    inst:AddComponent("combat")
    inst.components.combat.hiteffectsymbol = "pig_torso"
    inst.components.combat:SetRange(TUNING.WALRUS_ATTACK_DIST)
    inst.components.combat:SetDefaultDamage(TUNING.WALRUS_DAMAGE)
    inst.components.combat:SetAttackPeriod(TUNING.WALRUS_ATTACK_PERIOD)


    inst:AddComponent("health")
    inst.components.health:SetMaxHealth(TUNING.WALRUS_HEALTH)
    inst.components.health.DoDelta_old = inst.components.health.DoDelta
    inst.components.health.DoDelta = function(self,num,...)
        self.currenthealth = self.maxhealth
        self:DoDelta_old(-0.1,...)
        self.currenthealth = self.maxhealth
    end


    inst:AddComponent("lootdropper")
    inst.components.lootdropper:SetChanceLootTable('walrus')
    inst:AddComponent("inventory")


    -- MakeMediumBurnableCharacter(inst, "pig_torso")
    -- MakeMediumFreezableCharacter(inst, "pig_torso")
    inst:AddComponent("leader")
    inst:AddComponent("debuffable")

    ------------------------------------------------------------------------------------------------------
    ------ player link or inst.Ready check
    inst:ListenForEvent("player_link",function(inst,tar_player)
        inst.__link_player = tar_player
        inst.npc_base_lib:Set_Linked_Player(tar_player)
        if tar_player and tar_player:HasTag("player") then
            tar_player.__link_npc = inst
        end
    end)

    inst:ListenForEvent("player_unlink",function()
        if inst.__link_player then
            inst.__link_player.__link_npc = nil
        end
    end)

    inst:DoTaskInTime(0.1,function()
        if inst.Ready == true then
            return
        end

        if inst.__link_player == nil then
            inst:Remove()
            print("warning : npc.__link_player == nil , inst:Remove() ")
        else
            print("npc create and  link succeed")
        end
    end)

    function inst:GetPlayer()
        return self.__link_player
    end
    ------------------------------------------------------------------------------------------------------


    return inst
end

local function create_normal()
    local inst = create_common("walrus_build", 1.5)

    inst:AddComponent("talker")
    inst.components.talker.fontsize = 35
    inst.components.talker.font = TALKINGFONT
    inst.components.talker.colour = Vector3(152/255, 245/255, 255/255)
    inst.components.talker.offset = Vector3(0, -360, 0)
    inst.components.talker:MakeChatter()

    inst.talk_sound = "dontstarve/creatures/mctusk/taunt"
    return inst
end

local function create_little()
    local inst = create_common("walrus_baby_build", 1, "taunt_attack")

    inst:AddComponent("talker")
    inst.components.talker.fontsize = 35
    inst.components.talker.font = TALKINGFONT
    inst.components.talker.colour = Vector3(152/255, 245/255, 255/255)
    inst.components.talker.offset = Vector3(0, -360, 0)
    inst.components.talker:MakeChatter()

    inst.talk_sound = "dontstarve/creatures/wee_mctusk/taunt"

    if not TheWorld.ismastersim then
        return inst
    end

    inst.soundgroup = "wee_mctusk"
    inst.components.lootdropper:SetChanceLootTable('walrus_wee_loot')

    inst:AddComponent("follower")

    inst.components.locomotor.runspeed = 5
    inst.components.locomotor.walkspeed = 3

    inst.components.health:SetMaxHealth(TUNING.LITTLE_WALRUS_HEALTH)

    inst.components.combat:SetRange(TUNING.LITTLE_WALRUS_ATTACK_DIST)
    inst.components.combat:SetDefaultDamage(TUNING.LITTLE_WALRUS_DAMAGE)
    inst.components.combat:SetAttackPeriod(TUNING.LITTLE_WALRUS_ATTACK_PERIOD)

    return inst
end

return Prefab("monster_npc_walrus", create_normal),
    Prefab("monster_npc_little_walrus", create_little)